//
//  KDHealthPage.m
//  KDExample
//
//  Created by YeQing on 2017/3/8.
//  Copyright © 2017年 naruto. All rights reserved.
//

#import "KDHealthPage.h"
#import "KDHealthStore.h"

@interface KDHealthPage ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray            *listHealthSource;
@property (nonatomic, strong) UITableView               *tbHealth;
@property (nonatomic, strong) KDHealthStore             *healthStore;
@end

@implementation KDHealthPage
#pragma mark - system
+(void)load
{
    [kNBCURLRouterInstance registerScheme:@"KDHealthPage" scheme:@"health"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setTitle:self.nbcRouterParams[@"name"]];
    [self.view addSubview:self.tbHealth];
    @BCWeakObj(self);
    [self.healthStore requestStepCount:^(BOOL success, NSInteger stepCount) {
        @BCStrongObj(self);
        [self reloadHealthStepCount:success stepCount:stepCount];
    }];
    
}
-(void)dealloc
{
    BCLogDealloc();
}


#pragma mark - getter
-(UITableView *)tbHealth
{
    if(!_tbHealth){
        _tbHealth = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kBCSCREEN_WIDTH,kBCSCREEN_HEIGHT) style:UITableViewStylePlain];
        _tbHealth.dataSource = self;
        _tbHealth.delegate = self;
        _tbHealth.allowsMultipleSelection = NO;
        _tbHealth.backgroundColor = [UIColor whiteColor];
        _tbHealth.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return _tbHealth;
}
-(NSMutableArray *)listHealthSource
{
    if(!_listHealthSource){
        _listHealthSource = [[NSMutableArray alloc] initWithCapacity:0];
        [_listHealthSource addObject:@{@"id":@"step",@"name":@"步数",@"value":@"暂无"}];
    }
    return _listHealthSource;
}
-(KDHealthStore *)healthStore
{
    if(!_healthStore){
        _healthStore = [KDHealthStore new];
    }
    return _healthStore;
}

#pragma mark - 刷新步数
- (void)reloadHealthStepCount:(BOOL )success stepCount:(NSInteger )stepCount
{
    [self.listHealthSource removeAllObjects];
    NSString *strStepCount = nil;
    if(success){
        strStepCount = [NSString stringWithFormat:@"%ld 步",(long)stepCount];
    }
    else{
        strStepCount = @"暂无";
    }
    [self.listHealthSource addObject:@{@"id":@"step",@"name":@"步数",@"value":strStepCount}];
    [self.tbHealth reloadData];
}



#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listHealthSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.listHealthSource.count <= indexPath.row){
        return nil;
    }
    NSDictionary *functionItem = [self.listHealthSource objectAtIndex:indexPath.row];
    static NSString *kCellIdentifier = @"kBPHealthListTableViewCellIdentifierKey";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier];
    }
    cell.textLabel.text = functionItem[@"name"];
    cell.detailTextLabel.text = functionItem[@"value"];
    return cell;
}
@end
